/**
 *  Copyright 2009-2010 Steven Ottenhoff (Dual-IT)
 * 
 *  This file is part of Clazzified.
 *
 *  Clazzified is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Clazzified is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Clazzified.  If not, see http://www.gnu.org/licenses/gpl.
 * 
 */

package nl.dualit.clazzified.core.model.attribute;

import java.util.List;
import java.util.Map;

import nl.dualit.clazzified.core.model.AbstractBuilder;
import nl.dualit.clazzified.core.model.attribute.constraint.IConstraint;
import nl.dualit.clazzified.core.model.clazz.Clazz;
import nl.dualit.clazzified.store.AttributeData;
import nl.dualit.clazzified.store.clazz.AttributeFields;
import nl.dualit.clazzified.store.clazz.ConstraintFields;

import org.apache.log4j.Logger;

/**
 * This class builds attribute instances including the attribute constraints.
 * 
 * @author Steven Ottenhoff
 *
 */
public class AttributeBuilder extends AbstractBuilder {
	
	private static final Logger log = Logger.getLogger( AttributeBuilder.class );
	
	public IAttribute getInstance( Clazz clazz, AttributeData attributeData ){
		
		Map<AttributeFields,String> attributeMap = attributeData.getAttributeMap();
		
		
		IAttribute attribute = getInstance( clazz, ( attributeMap.get( AttributeFields.ATTRIBUTE_ID ) != null )? Long.valueOf( attributeMap.get( AttributeFields.ATTRIBUTE_ID ) ) : null,
				attributeMap.get( AttributeFields.ATTRIBUTE_NAME ),
				attributeMap.get( AttributeFields.ATTRIBUTE_DESCRIPTION ),
				attributeMap.get( AttributeFields.TYPE_NAME ), null );
		
		// Initialize the constraints
		for( Map<ConstraintFields,String> constraintMap : attributeData.getConstraints() ){
			
			attribute.addConstraint( getConstraint( attribute, constraintMap ) );
		}
		
		return attribute;
	}
	
	public IAttribute getInstance( Clazz clazz, Long attributeId, String attributeName, String description, String typeName, List<IConstraint> constraints ){
		
		if( clazz == null )
			throw new NullPointerException( "Attribute clazz is null" );
		if( attributeName == null )
			throw new NullPointerException( "Attribute name is null" );
		if( typeName == null )
			throw new NullPointerException( "Attribute type name is null" );
		AttributeTypes type = AttributeTypes.getType( typeName );
		if( type == null )
			throw new NullPointerException( "Type name " + type + " is not reconized" );
		
		switch( type ){
			
		case TEXT:
			return new TextAttribute(clazz, null, attributeName, description, constraints );
			
		}
		
		
		return null;
	}
	
	public IConstraint getConstraint( IAttribute attribute, Map<ConstraintFields,String> constraintMap ){
		
		return null;
	}
	
}
